package PLAYER;


import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;


public class ConnectingWindow extends JFrame implements ActionListener
{
	JTextField host_name, port_number, nick_field;
	JLabel host, port, nick_name;
	JButton connect;
	int port_nr = 0;
	MainThread mainThread;

	public ConnectingWindow()
	{
		this.setSize(440,330);
		this.setLocationRelativeTo(null);
		this.setTitle("Badugi Connecting Window");
		this.getContentPane().setBackground(new Color(61,61,61));
		this.setResizable(false);
		this.setLayout(null);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		host = new JLabel("HOST: ");
		host.setBounds(80, 60, 80, 40);
		host.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));
		host.setForeground(new Color(121,121,121));
		add(host);
		
		host_name = new JTextField();
		host_name.setBounds(160, 60, 200, 40);
		host_name.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 25));
		host_name.setBackground(new Color(92,92,92));
		host_name.setForeground(new Color(40,40,40));
		host_name.setBorder(BorderFactory.createEmptyBorder());
		add(host_name);
		
		port = new JLabel("PORT: ");
		port.setBounds(80, 110, 80, 40);
		port.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));
		port.setForeground(new Color(121,121,121));
		add(port);
		
		port_number = new JTextField();
		port_number.setBounds(160, 110, 200, 40);
		port_number.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 25));
		port_number.setBackground(new Color(92,92,92));
		port_number.setForeground(new Color(40,40,40));
		port_number.setBorder(BorderFactory.createEmptyBorder());
		add(port_number);
		
		nick_name = new JLabel("NICK: ");
		nick_name.setBounds(80, 160, 80, 40);
		nick_name.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));
		nick_name.setForeground(new Color(121,121,121));
		add(nick_name);
		
		nick_field = new JTextField();
		nick_field.setBounds(160, 160, 200, 40);
		nick_field.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 25));
		nick_field.setBackground(new Color(92,92,92));
		nick_field.setForeground(new Color(40,40,40));
		nick_field.setBorder(BorderFactory.createEmptyBorder());
		add(nick_field);
		
		connect = new JButton("Connect");
		connect.setBounds(150, 230, 140, 40);
		connect.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 20));
		connect.setBackground(new Color(120,120,120));
		connect.setForeground(new Color(61,61,61));
		connect.setBorder(BorderFactory.createEmptyBorder());

		//Kod poniżej pozwoli na zmiane koloru buttona
		connect.addMouseListener(new java.awt.event.MouseAdapter() {
		    public void mouseEntered(java.awt.event.MouseEvent evt) {
		        connect.setBackground(new Color(180,180,180));
		    }
		    
		    public void mouseExited(java.awt.event.MouseEvent evt)
		    {
		    	connect.setBackground(new Color(120,120,120));
		    }
		});
		connect.addActionListener(this);
		add(connect);

	}
	

	public void actionPerformed(ActionEvent e)
	{


		if(e.getSource()==connect)
		{

			try
			{
				port_nr = Integer.parseInt(port_number.getText());
				
				if(port_nr<=0)
				{
					JOptionPane.showMessageDialog(null, "This number must be higher than 0");
					port_number.setText("");
				}
			}
			catch(NumberFormatException NFE)
			{
				JOptionPane.showMessageDialog(null,"Typed text is not number");
				port_number.setText("");
			}
			System.out.println(nick_field.getText());
			mainThread = new MainThread(port_nr, host_name.getText(), nick_field.getText(), this);
			mainThread.start();
		}
	}
}
